package org.rowan.adi.components.comp2d;

import java.util.LinkedList;
import org.rowan.adi.Vector;
import org.rowan.adi.components.comp2d.polygon.Polygon;
import org.rowan.adi.linearalgebra.LinearAlgebra2D;

/**
 * The Circle class provides functionality for a circle in two dimensional space.
 *
 * @author Robert Russell
 * @version 1.0
 */
public class Circle extends Component2D {

    private double radius; // The radius of the circle.

    /**
     * Constructs an instance of Circle.
     * @param location the location of the Circle.
     * @param radius the radius of the Circle.
     * @throws IllegalArgumentException the radius is not a positive value.
     */
    public Circle(Vector location, double radius) {
        if (radius <= 0) {
            throw new IllegalArgumentException("Invalid radius. The radius must be a positive value.");
        }
        this.radius = radius;
        LinkedList<Point2D> points = new LinkedList<Point2D>();
        points.add(new Point2D(location));
        super.setListofPoints(points);
        super.setLocation(location);
        super.setTrajectory(Vector.ZERO_VECTOR);
    }

    /**
     * Computes the area of the Circle.
     * @return the area of the Circle.
     */
    public double computeArea() {
        return Math.PI * Math.pow(radius, 2);
    }

    /**
     * Gets the radius of the Circle.
     * @return the radius of the Circle.
     */
    public double getRadius() {
        return this.radius;
    }

    /**
     * Computes the cross-sectional area of this Circle with the Circle instance given.
     * @param circle an instance of Circle.
     * @return the cross-sectional area of this Circle with the Circle instance given.
     */
    public final double intersectCircle(Circle circle) {
        return LinearAlgebra2D.intersectArea(this, circle);
    }

    /**
     * Computes the cross-sectional area of this Circle with the Polygon instance given.
     * @param polygon an instance of Polygon.
     * @return the cross-sectional area of this Circle with the Polygon instance given.
     */
    public final double intersectPolygon(Polygon polygon) {
        return LinearAlgebra2D.intersectArea(this, polygon);
    }

    /**
     * Sets the radius of the Circle to a given value.
     * @param radius the new value for the radius.
     */
    public void setRadius(double radius) {
        if (radius <= 0) {
            throw new IllegalArgumentException("Invalid radius. The radius must be a positive value.");
        }

        this.radius = radius;
    }
}
